package ch01.Shangji;

import ch01.entity.Person;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class Shangji1 {
    public static void main(String[] args) {

        Class cla = Person.class;
        String simpleName = cla.getSimpleName();

        Class superClass = cla.getSuperclass();
        System.out.println("类名："+cla.getName());
        System.out.println("被"+superClass.getName()+"类继承");

        Class[] interfaces = cla.getInterfaces();
        System.out.print("实现的接口：");
        for (Class c : interfaces) System.out.print(c.getName()+" ");

        int modifier = cla.getModifiers();
        System.out.print("\n访问修饰符是："+((modifier & Modifier.PUBLIC) == Modifier.PUBLIC ? "public," : "default(package),"));
        if ((modifier & Modifier.FINAL)==Modifier.FINAL)
            System.out.println("这个类是 final 的");
        if ((modifier & Modifier.ABSTRACT)==Modifier.ABSTRACT)
            System.out.println("这是一个抽象类");
        if ((modifier & Modifier.INTERFACE)==Modifier.INTERFACE)
            System.out.println("这是一个接口");

        System.out.println(cla.getName()+"类的构造方法：");
        Constructor[] constructors = Person.class.getDeclaredConstructors();
        for (Constructor con : constructors){
            System.out.print("访问修饰符：");
            int modifier2 = con.getModifiers();

            if ((modifier2 & Modifier.PUBLIC) == Modifier.PUBLIC)
                System.out.print("public");
            else if ((modifier2 & Modifier.PROTECTED) == Modifier.PROTECTED)
                System.out.print("protected");
            else if ((modifier2 & Modifier.PRIVATE) == Modifier.PRIVATE)
                System.out.print("private");
            else
                System.out.print("default(package)");

            //获取构造方法的参数列表
            Class[] params = con.getParameterTypes();
            if(params.length == 0){
                System.out.println(" 无参数");
            }else{
                System.out.print(" 参数为：[");
                for (int i = 0;i<params.length;i++){
                    if(i != 0)
                        System.out.print(", ");
                    System.out.print(params[i].getName());
                }
                System.out.println("]");
            }
        }

        System.out.println(cla.getName()+"类的属性：");
        Field[] fields = Person.class.getDeclaredFields();
        for (Field field : fields) {

            int modifiers = field.getModifiers();
            String mod = "";
            if((modifiers & Modifier.STATIC) == Modifier.STATIC)
                mod = "static";
            if((modifiers & Modifier.FINAL) == Modifier.FINAL)
                mod = "final";
            if ((modifiers & Modifier.PUBLIC) == Modifier.PUBLIC)
                System.out.print("public ");
            else if ((modifiers & Modifier.PROTECTED) == Modifier.PROTECTED)
                System.out.print("protected ");
            else if ((modifiers & Modifier.PRIVATE) == Modifier.PRIVATE)
                System.out.print("private ");
            else
                System.out.print("default(package) ");

            System.out.println(field.getType().getName()+" "+mod+" "+field.getName());
        }

        System.out.println(cla.getName()+"类的方法：");
        Method[] methods = Person.class.getDeclaredMethods();

        for (Method method : methods){
            System.out.println("方法名："+method.getName());
            System.out.println("返回值类型："+method.getReturnType().getName());

            Class[] params = method.getParameterTypes();

            System.out.print("访问修饰符：");
            int modifier3 = method.getModifiers();
            if((modifier3 & Modifier.STATIC) == Modifier.STATIC)
                System.out.println(" 静态方法");
            if((modifier3 & Modifier.FINAL) == Modifier.FINAL)
                System.out.println(" final 方法");
            if((modifier3 & Modifier.ABSTRACT) == Modifier.ABSTRACT)
                System.out.println(" 抽象方法");
            if((modifier3 & Modifier.SYNCHRONIZED) == Modifier.SYNCHRONIZED)
                System.out.println(" 同步方法");

            Class declaringClass = method.getDeclaringClass();

            Class[] exceptions = method.getExceptionTypes();
            if(exceptions.length > 0){
                System.out.print(" 异常：[");
                for (int i = 0;i<exceptions.length;i++){
                    if(i != 0){
                        System.out.print(", ");
                    }
                    System.out.print(exceptions[i].getName());
                }
                System.out.println("]");
            }
        }
    }
}
